package com.leoman.spinningmachine.tab1;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;
import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.util.ToastUtil;
import com.leoman.helper.view.NoScrollGridView;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.base.BaseFragment;
import com.leoman.spinningmachine.bean.BannerBean;
import com.leoman.spinningmachine.bean.RmhdBean;
import com.leoman.spinningmachine.bean.ZxdtBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.main.MainActivity;
import com.leoman.spinningmachine.tab3.CardRecordActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.leoman.spinningmachine.constant.Urls.INDEX_URL;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    @BindView(R.id.rl_new_dt)
    RelativeLayout rl_new_dt;
    @BindView(R.id.banner)
    BGABanner banner;
    @BindView(R.id.gridView)
    NoScrollGridView gridView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.viewFlipper)
    ViewFlipper viewFlipper;

    private List<BannerBean> bannerList = new ArrayList<>();
    private FunctionAdapter adapter1;
    private int[] icons = {R.drawable.ic_function1, R.drawable.ic_function2, R.drawable.ic_function3, R.drawable.ic_function4,
            R.drawable.ic_function5, R.drawable.ic_function6, R.drawable.ic_function7, R.drawable.ic_function8};
    private String[] names = {"新闻动态", "考勤打卡", "请假审批", "安防系统", "门禁系统", "链接OA", "链接ERP", "全部功能"};
    private HotAdapter adapter2;
    private List<ZxdtBean> zxdtLists = new ArrayList<>();
    private List<RmhdBean> rmhdLists = new ArrayList<>();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fg_home2;
    }

    @Override
    protected void initView(View view) {
        adapter1 = new FunctionAdapter(getActivity(), icons, names);
        gridView.setAdapter(adapter1);
        gridView.setOnItemClickListener(this);

        ((BaseActivity) getActivity()).setLayoutManager(recyclerView, LinearLayoutManager.VERTICAL, false);
        adapter2 = new HotAdapter(getActivity(), R.layout.item_hot, rmhdLists);
        recyclerView.setAdapter(adapter2);
    }

    @Override
    protected void initData() {
        DialogUtils.getInstance().show(getActivity());
        WebServiceApi.getInstance().queryZxDt("", this, 1);
        WebServiceApi.getInstance().queryRmhd("", this, 2);
        WebServiceApi.getInstance().banner(this, 3);
        adapter2.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                ((BaseActivity) getActivity()).gotoFlagOtherActivity(Detail22Activity.class, rmhdLists.get(position).getId());
            }
        });
    }

    /**
     * 最新动态
     *
     * @param zxdtLists
     */
    private void setViewFlipper(List<ZxdtBean> zxdtLists) {
        if (zxdtLists != null && zxdtLists.size() >= 2) {
            final View itemview = LayoutInflater.from(getActivity()).inflate(R.layout.item_viewflipper_list, null);
            TextView tv_note = (TextView) itemview.findViewById(R.id.tv_note);
            TextView tv_note_content = (TextView) itemview.findViewById(R.id.tv_note_content);
            RelativeLayout rl1 = (RelativeLayout) itemview.findViewById(R.id.rl1);

            TextView tv_note2 = (TextView) itemview.findViewById(R.id.tv_note2);
            TextView tv_note_content2 = (TextView) itemview.findViewById(R.id.tv_note_content2);
            RelativeLayout rl2 = (RelativeLayout) itemview.findViewById(R.id.rl2);

            ZxdtBean bean1 = zxdtLists.get(0);
            ZxdtBean bean2 = zxdtLists.get(1);

            tv_note.setText("" + bean1.getTypeName());
            tv_note_content.setText("" + bean1.getTitle());
            tv_note_content.setMaxLines(1);
            tv_note_content.setEllipsize(TextUtils.TruncateAt.END);

            tv_note2.setText("" + bean2.getTypeName());
            tv_note_content2.setText("" + bean2.getTitle());
            tv_note_content2.setMaxLines(1);
            tv_note_content2.setEllipsize(TextUtils.TruncateAt.END);
            viewFlipper.addView(itemview, 0);
            viewFlipper.startFlipping();

            rl1.setTag(R.id.text1, bean1);
            rl1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ZxdtBean bean1 = (ZxdtBean) v.getTag(R.id.text1);
                    ((MainActivity) getActivity()).gotoFlagOtherActivity(Detail22Activity.class, bean1.getId());
                }
            });
            rl2.setTag(R.id.text1, bean2);
            rl2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ZxdtBean bean2 = (ZxdtBean) v.getTag(R.id.text1);
                    ((MainActivity) getActivity()).gotoFlagOtherActivity(Detail22Activity.class, bean2.getId());
                }
            });
        } else {
            ToastUtil.showToast(getActivity(), "服务器返回错误", Toast.LENGTH_SHORT);
        }
    }


    @OnClick(R.id.rl_new_dt)
    public void onClicked() {
//        ((BaseActivity)getActivity()).gotoOtherActivity(DetailActivity.class);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i) {
            case 0://新闻动态
                ((BaseActivity) getActivity()).gotoOtherActivity(NewsActivity.class);
                break;
            case 1://考勤打卡
                ((BaseActivity) getActivity()).gotoOtherActivity(CardRecordActivity.class);
                break;
            case 2://请假审批
                ((BaseActivity) getActivity()).gotoOtherActivity(AskLeaveActivity.class);
                break;
            case 3://安防系统
                int cameraq = SharedPreferencesUtils.getInstance().getInt(Constant.CAMERAQ);
                if (cameraq == 1) {
                    ((BaseActivity) getActivity()).gotoFlagTitleActivity(NoFunActivity.class, "安防系统", 1);
                } else {
                    ToastUtil.showToast(getActivity(), "暂无权限，请联系后台管理人员添加", Toast.LENGTH_SHORT);
                }

                break;
            case 4://门禁系统
                ((BaseActivity) getActivity()).gotoOtherActivity(EntranceGuardActivity.class);
                break;
            case 5://链接OA
//                ((BaseActivity)getActivity()).gotoFlagTitleActivity(NoFunActivity.class,"链接OA",2);
                break;
            case 6://链接ERP
//                ((BaseActivity)getActivity()).gotoFlagTitleActivity(NoFunActivity.class,"链接ERP",3);
                break;
            case 7://全部
                ((BaseActivity) getActivity()).gotoOtherActivity(AllFunctionActivity.class);
                break;
        }
    }

    /**
     * 初始化轮播图
     */
    private void initBanner() {
        banner.setAdapter(new BGABanner.Adapter() {
            @Override
            public void fillBannerItem(BGABanner banner, View view, Object model, int position) {
                if (!getActivity().isFinishing()) {
                    BannerBean bean = (BannerBean) model;
                    Glide.with(getActivity()).load(INDEX_URL + bean.getImage()).
                            placeholder(R.mipmap.ic_home_test).
                            error(R.mipmap.ic_home_test).
                            dontAnimate().thumbnail(0.1f).into((ImageView) view);
                }
            }
        });
        banner.setData(bannerList, null);

        banner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {

            }
        });
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        if (1 == tag) {
            zxdtLists = apiResponse.data.list;
            setViewFlipper(zxdtLists);
        } else if (2 == tag) {
            rmhdLists = apiResponse.data.list;
            adapter2.setData(rmhdLists);
        } else if (3 == tag) {
            bannerList = apiResponse.data.list;
            initBanner();
        }
        DialogUtils.getInstance().cancel();
    }

}
